package com.ddpt.gateway.filter;

import com.ddpt.gateway.filter.base.BaseLimitFilter;
import com.ddpt.platform.common.constant.GlobalConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.UUID;

/**
 * 日志拦截
 */
@Slf4j
@Component
public class LoggerFilter extends BaseLimitFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String traceId = UUID.randomUUID().toString().replace("-","");
        String path = exchange.getRequest().getURI().getPath();
        String token = exchange.getRequest().getHeaders().getFirst("token");
        log.info("[{}]接口[{}],客户端ip:[{}],携带token:[{}]",traceId,path,getIp(exchange.getRequest()),token);
        ServerHttpRequest request = exchange.getRequest().mutate().header(GlobalConstant.header_log_trance_id, traceId).build();
        return chain.filter(exchange.mutate().request(request).build());
    }

    @Override
    public int getOrder() {
        return -97;
    }
}
